$(document).ready(function(){
	eventBind();
	genDictSelect('ResourceType','resource_category1','1');
	$('#file').fileinput({//附件上传
		language : 'zh',
		showUpload : false,
		uploadAsync: true, //默认异步上传
		allowedFileExtensions : [ 'pdf', 'mp4', 'mp3' ],
		dropZoneEnabled: true,//是否显示拖拽区域
		maxFileCount: 1, //表示允许同时上传的最大文件个数
		enctype: 'multipart/form-data',
	    validateInitialCount:true
	})
});
// 事件绑定
function eventBind(){
	$('#cancel').click(function(){
		BootstrapDialog.closeAll();
	})
	$('#save').on('click',uploadResource);
}

//新增教育资源
function uploadResource(){
	if(!$("#studyForm").validationForm()){
		return;
	}
	if($('#fileName').val()==''){
		customTimeOutDialog('信息提示','请选择上传文件后再保存',null,1000);
	}else{
		$.getApiClient('/study/save').post('#studyForm', function(params){
			return $.extend(params, {
				resource_name: $('#resource_name1').val(),
				resource_type: $('#resource_type1').val(),
				resource_desc: $('#resource_desc1').val(),
				resource_category: $('#resource_category1').val()
			});
		}).done(function(data){
			if(data.code == "0"){
				var id=data.payload.id;
				upload(id);
			}else{
				customAlertDialog(data.message, '错误提示', null, null);
			}
		});
	}
}
//上传附件
function upload(id){
	$.getApiClient('/attachment/upload/{id}','multipart/form-data').setQuery({id: id}).uploadForm('#fileFrom').done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','资源上传成功',null,2000);
			pageQuery();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}
